﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.Core.Contacts.Services;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Core.Enums;
using MvcMall.Framework.UnitOfWork;

namespace MvcMall.AdminWeb.Manage.StoreApplies
{
    public partial class Details : ManageBasePage
    {


        private IStoreApplyTask _storeApplyTask;

        private IFileService _fileService;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("ShenHeStoreApply");
            _storeApplyTask = GetInstance<IStoreApplyTask>();
            _fileService = GetInstance<IFileService>();
            if (!IsPostBack)
            {
                int id = -1;
                int.TryParse(Request["ID"], out id);
                if (id <= 0)
                {
                    ShowMsgAndReturn("末读取到该数据");
                    return;
                }
                var mode = _storeApplyTask.Get(id);
                if (mode == null)
                {
                    ShowMsgAndReturn("末读取到该数据");
                    return;
                }

                ltlId.Text = mode.Id.ToString();
                ltlHuiYuan.Text = mode.User.UserName;
                ltlLevery.Text = mode.StoreGrade.GradeName;
                ltlUserName.Text = mode.OwnerName;
                ltlDianName.Text = mode.StoreName;
                ltlDianType.Text = mode.StoreCat.CatName;
                ltlDianState.Text = mode.StoreType.ToString();
                ltlShen.Text = mode.StoreApplyStatus.ToString();
                ltlTuiJian.Text = mode.PromoteUser != null ? mode.PromoteUser.UserName : "";
                ltlUserSate.Text = mode.IdentityCardId;
                string imgUrl = mode.IdentityCardImage;
                ltlStateImg.Text = string.Format("<img style=\" width:500px; height:350px;\"  src=\"{0}\" />", imgUrl);
                string yingImgUrl = mode.LicenseImage;
                ltlYingImg.Text = string.Format("<img style=\" width:500px; height:350px;\"  src=\"{0}\" />", yingImgUrl);
                ltlCreateDate.Text = mode.CreateTime.ToString();
                if (mode.StoreApplyStatus == StoreApplyStatus.待审核)
                {
                    btnOK.Style["display"] = "block";
                    btnNo.Style["display"] = "block";
                }
                else
                {
                    btnOK.Style["display"] = "none";
                    btnNo.Style["display"] = "none";
                }
            }

        }




        protected void Button2_Click(object sender, EventArgs e)
        {
            #region///审核不通过
            var id = int.Parse(Request["ID"]);
            var model = _storeApplyTask.Get(id);
            if (model.StoreApplyStatus == StoreApplyStatus.待审核)
            {
                using (var work = UnitOfWork.Begin())
                {

                    var result = _storeApplyTask.Deny(model.Id);
                    work.Commit();
                    ltlShen.Text = result.StoreApplyStatus.ToString();
                }
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('操作成功！');location.href='Index.aspx';", true);
                return;
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('只允许待审核申请设置为审核不通过！');location.href='Index.aspx';", true);
                return;
            }


            #endregion

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region///审核通过

            var id = int.Parse(Request["ID"]);
            var model = _storeApplyTask.Get(id);
            if (model.StoreApplyStatus == StoreApplyStatus.待审核)
            {
                using (var work = UnitOfWork.Begin())
                {

                    var result = _storeApplyTask.ShenHe(model.Id);
                    work.Commit();
                    ltlShen.Text = result.StoreApplyStatus.ToString();
                }
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('操作成功！');location.href='Index.aspx';", true);
                return;
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('只允许待审核申请设置为审核通过！');location.href='Index.aspx';", true);
                return;
            }

            #endregion
        }
    }
}